package com.example.testfrountcamera;

import java.util.List;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class FrontCameraTest extends Activity implements SurfaceHolder.Callback {
	private Camera mCamera01;
	private static final String TAG = "FrontCameraTest";
	private SurfaceView mSurfaceView01;
	private SurfaceHolder mSurfaceHolder01;
	private DisplayMetrics dm;
	private boolean mGetLayoutCount = false;

	protected void onPause() {
		try {
			if (mCamera01 != null) {
				mCamera01.stopPreview();
				mCamera01.release();
				mCamera01 = null;
			}
		} catch (Exception e) {
			Log.i(TAG, e.toString());
		}
		mGetLayoutCount = false;
		super.onPause();
	}

	@SuppressWarnings("deprecation")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.frontcamera);
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);

		try {
			mSurfaceHolder01 = mSurfaceView01.getHolder();
			mSurfaceHolder01.addCallback(FrontCameraTest.this);
			mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		} catch (Exception e) {
			Log.i(TAG, e.toString());
			e.printStackTrace();
		}
	}


	Size currentSize = null;
	public void surfaceChanged(SurfaceHolder surfaceholder, int format, int w,
			int h) {
		Log.i(TAG, "Surface Changed , w =" + w + ", h=" + h);
		if (!mGetLayoutCount) {
			
			ViewGroup.LayoutParams layout = mSurfaceView01.getLayoutParams();
			layout.width = dm.widthPixels;
			layout.height = dm.widthPixels * currentSize.width/currentSize.height;
			mSurfaceView01.setLayoutParams(layout);
			
			mGetLayoutCount = true;
		}
	}

	@SuppressWarnings("deprecation")
	private void startPreview() {
		try {
			Camera.Parameters parameters = mCamera01.getParameters();
			List<Size> sizes = parameters.getSupportedPreviewSizes();
			Size optimalSize = getOptimalPreviewSize(sizes);
			Log.i(TAG, "startPreview,  optimalSize.width = " + optimalSize.width + " , optimalSize.height = " + optimalSize.height);
			parameters.setPreviewSize(optimalSize.width, optimalSize.height);
			parameters.setPictureFormat(PixelFormat.JPEG);
			parameters.setAntibanding(Camera.Parameters.ANTIBANDING_50HZ);
			mCamera01.setParameters(parameters);
			mCamera01.setDisplayOrientation(90);
			
			currentSize = parameters.getPreviewSize();
			Log.i(TAG, "startPreview,  currentSize.width = " + currentSize.width + " , currentSize.height = " + currentSize.height);
			
			mCamera01.startPreview();

		} catch (Exception e) {
			Log.e(TAG, "-->> start preview, " + e.toString());
		}
	}

	public void surfaceCreated(SurfaceHolder surfaceholder) {
		Log.i(TAG, "Surface created");
		try {
			mCamera01 = Camera.open(0);
			mCamera01.setPreviewDisplay(mSurfaceHolder01);
			startPreview();
		} catch (Exception ex) {
			Log.e(TAG, "open front camera fail!");
		}
	}

	public void surfaceDestroyed(SurfaceHolder surfaceholder) {
		Log.i(TAG, "Surface Destroyed");
	}

	public Size getOptimalPreviewSize(List<Size> sizes) {
		double targetRatio = 1f*dm.heightPixels/dm.widthPixels;
		Log.i(TAG, "getPreviewSize, targetRatio = " + targetRatio +  ", dm.heightPixels = " +  dm.heightPixels + ", dm.widthPixels = " + dm.widthPixels);
		if (sizes == null) return null;
		
		Size optimalSize = null;
		double dTargetRatio = 10.0;
		
		for (Size size : sizes) {
			double delta= Math.abs(targetRatio - 1f*size.width / size.height);
			if(delta<dTargetRatio){
				optimalSize = size;
				dTargetRatio = delta;
			}
			Log.i(TAG, "getPreviewSize, dTargetRatio = " + dTargetRatio + ", size.width = " + size.width + ", size.height = " + size.height);
		}

		return optimalSize;
	}

}